//
//  NSString+BVideoCache.m
//  Bullet
//
//  Created by  on 2019/9/19.
//  Copyright © 2019 . All rights reserved.
//

#import "NSString+BVideoCache.h"

@implementation NSString (BVideoCache)

- (NSDate *)stringToDate:(NSString *)formate{
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    f.dateFormat       = formate;
    return [f dateFromString:self];
}

- (NSString *)stringToShortNumber{
    NSInteger selfNum = [self intValue];
    if (selfNum >9999) {
        return [NSString stringWithFormat:@"%.1fW",(CGFloat)selfNum /10000.f];
    }else if (selfNum >999) {
        return [NSString stringWithFormat:@"%.1fK",(CGFloat)selfNum /1000.f];
    }else{
        return self;
    }
}

- (NSRange)fixLinkRangeWithMatch:(NSTextCheckingResult *)match{
    NSRange urlRange = [self rangeOfString:match.URL.absoluteString];
    if(![self containsString:match.URL.absoluteString]){
        NSString *replaceStr = [match.URL.absoluteString stringByReplacingOccurrencesOfString:@"http"
                                                                                   withString:@"https"];
        urlRange = [self rangeOfString:replaceStr];
    }
    return urlRange;
}

- (CGSize)sizeWithFont:(UIFont *)font fitSize:(CGSize)fitSize{
    NSDictionary *dic = @{NSFontAttributeName :  font};
    CGSize size = [self boundingRectWithSize:fitSize
                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                  attributes:dic
                                     context:nil].size;
    return size;
}

- (BOOL)isNumberStr{
    NSString *regexStr = @"^[0-9.]+";
    NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexStr];
    return [numberTest evaluateWithObject:[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}

- (NSString *)urlUtf8EncodeStr{
    NSString *charactersToEscape = @"'\"`<>()[]{}| \u4e00-\u9fa5";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    return [self stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
}

- (BOOL)isValidateTitleStr{
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:self];
    return !isMatch;
}

- (BOOL)containsEmoji
{
    __block BOOL returnValue = NO;
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        if(substring.length > 0){
            const unichar hs = [substring characterAtIndex:0];
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (substring.length > 1) {
                    const unichar ls = [substring characterAtIndex:1];
                    const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        returnValue = YES;
                    }
                }
            } else if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                if (ls == 0x20e3) {
                    returnValue = YES;
                }
            } else {
                if (0x2100 <= hs && hs <= 0x27ff) {
                    returnValue = YES;
                } else if (0x2B05 <= hs && hs <= 0x2b07) {
                    returnValue = YES;
                } else if (0x2934 <= hs && hs <= 0x2935) {
                    returnValue = YES;
                } else if (0x3297 <= hs && hs <= 0x3299) {
                    returnValue = YES;
                } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                    returnValue = YES;
                }
            }
        }
    }];
    return returnValue;
}

+ (NSString *)hexStringWithData:(NSData *)data
{
    const unsigned char *dataBuffer = (const unsigned char *)[data bytes];
    if(!dataBuffer){
        return nil;
    }
    NSUInteger dataLength = [data length];
    NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
    for(int i = 0; i < dataLength; i++){
        [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
    }
    NSString* result = [NSString stringWithString:hexString];
    return result;
}

+ (NSString *)secondsToTimeStr:(NSInteger)seconds{
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%.2ld",seconds / 60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%.2ld",seconds % 60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    return format_time;
}

+ (NSString *)memerySizeStrWithSize:(long long)size{
    if(size < 1024 * 1024){
        return [NSString stringWithFormat:@"%.2fK",(CGFloat)size / 1024.f];
    }
    else
    {
        return [NSString stringWithFormat:@"%.2fM",(CGFloat)size / (1024.f * 1024.f)];
    }
}

@end
